﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tank.Properties;
namespace Tank
{
    class MyTank : Moveing
    {
        public bool IsMoveing { set; get; }
        public int HP { set; get; }
        private int originalX;
        private int originalY;
        public MyTank(int X, int y, int speet, Direction dir)
        {
            this.X = X;
            this.Y = y;
            this.Speet = speet;
            originalX = X;
            originalY = y;
            this.BitmapDown = Resources.MyTankDown;
            this.BitmapLeft = Resources.MyTankLeft;
            this.BitmapRight = Resources.MyTankRight;
            this.BitmapUp = Resources.MyTankUp;
            this.Dir = dir;
            HP = 3;
        }
        // 键盘按下
        public void KeyDown(KeyEventArgs e)
        {
            
            // keys是枚举类型
            switch (e.KeyCode)
            {
                case Keys.W:
                    Dir = Direction.Up;
                    IsMoveing = true;
                    break;
                case Keys.S:
                    Dir = Direction.down;
                    IsMoveing = true;
                    break;
                case Keys.A:
                    Dir = Direction.Left;
                    IsMoveing = true;
                    break;
                case Keys.D:
                    Dir = Direction.right;
                    IsMoveing = true;
                    break;
                case Keys.Space:
                    // 发射子弹
                    Attack();
                    break;
            }
        }
        // 发射子弹方法
        private void Attack()
        {
            int x = this.X;
            int y = this.Y;
            switch (Dir)
            {
                case Direction.Up:
                    x = x + this.Width / 2;
                    break;
                case Direction.down:
                    x = x + Width / 2;
                    y += Height;
                    break;
                case Direction.Left:
                    y = y + Height / 2;
                    break;
                case Direction.right:
                    x += Width;
                    y = y + Height / 2;
                    break;
            }

            GameObjectManager.CreateBullet(x, y, Tag.MyTank, Dir);
        }


        // 键盘抬起
        public void KeyUp(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.W:
                    Dir = Direction.Up;
                    IsMoveing = false;
                    break;
                case Keys.S:
                    Dir = Direction.down;
                    IsMoveing = false;
                    break;
                case Keys.A:
                    Dir = Direction.Left;
                    IsMoveing = false;
                    break;
                case Keys.D:
                    Dir = Direction.right;
                    IsMoveing = false;
                    break;
            }
        }
        public override void Update()
        {

            MoveCheck();
            Move();
            base.Update();
        }
        // 坦克越界检查
        private void MoveCheck()
        {
            #region 检测是否越界
            if (Dir == Direction.Up)
            {
                if (Y - Speet < 0)
                {
                    IsMoveing = false;
                    return;
                }
            }
            else if (Dir == Direction.down)
            {
                if (Y + Speet + Height > 450)
                {
                    IsMoveing = false;
                    return;
                }

            }
            else if (Dir == Direction.Left)
            {
                if (X - Speet < 0)
                {
                    IsMoveing = false;
                    return;
                }
            }
            else
            {
                if (X + Speet + Width > 450)
                {
                    IsMoveing = false;
                    return;

                }
            }
            #endregion
            Rectangle rect = GetRectangle();
            switch (Dir)
            {
                case Direction.Up:
                    rect.Y -= Speet;
                    break;
                case Direction.down:
                    rect.Y += Speet;
                    break;
                case Direction.Left:
                    rect.X -= Speet;
                    break;
                case Direction.right:
                    rect.X += Speet;
                    break;
            }
            // 两种墙体都不能碰撞

            if (GameObjectManager.IsCollidedWall(rect) != null)
            {
                IsMoveing = false; return;
            }
            if (GameObjectManager.IsCollidedSteel(rect) != null)
            {
                IsMoveing = false; return;
            }
            if (GameObjectManager.IsCollidedBoss(rect))
            {
                IsMoveing = false; return;
            }

        }
        // 坦克移动
        private void Move()
        {
            if (IsMoveing == false) return;
            switch (Dir)
            {
                case Direction.Up:
                    Y -= Speet;
                    break;
                case Direction.down:
                    Y += Speet;
                    break;
                case Direction.Left:
                    X -= Speet;
                    break;
                case Direction.right:
                    X += Speet;
                    break;
            }
        }
        public void TakeDamage()
        {
            HP--;
            // 没血之后，坦克重生
            if (HP < 0)
            {
                X = originalX;
                Y = originalY;
                HP = 3;
            }
        }
    }
}
